package org.csu.mypetstore.web.servlet;

import com.alibaba.fastjson.JSON;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;
import org.csu.mypetstore.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UpdateUsernameServlet", value = "/UpdateUsernameServlet")
public class UpdateUsernameServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        AccountService accountService=new AccountService();
        try {
            Account result=accountService.getAccountByUsername(username);
            //设置返回类型
            response.setContentType("text/json");
            //response.setContentType("text/plain");
            PrintWriter out=response.getWriter();


            Result result1=new Result();

            if(result==null)//用户名可用
            {   result1.setCode(0);
                result1.setMsg("Not Exist");
                //out.print("Not Exist");
            }
            else//用户名不可用
            {
                result1.setCode(1);
              result1.setMsg("Exist");
                //out.print("Exist");
            }
            String str= JSON.toJSONString(result1);
            out.print(str);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
